package com.shaoshaohuo.app.ui.ec;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.BusinessSellerDetail;
import com.shaoshaohuo.app.entity.BusinessSellerDetail.Supply;
import com.shaoshaohuo.app.entity.BusinessSellerDetailEntity;
import com.shaoshaohuo.app.utils.im.IMFactory;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.ProductGridviewAdapter1;
import com.shaoshaohuo.app.view.MeasureGridView;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.utils.DeviceHelper;

/**
 * @ClassName SupplierDetailActivity
 * @author linmu
 * @Decription 供货商详情
 * @2015-8-28 下午9:50:07
 */
public class SupplierDetailActivity extends BaseActivity {

	private TopbarView mTopbarView;
	
	private MeasureGridView mProductGv;
	private XListView mListView;
	
	private TextView mSupplyText;
	private TextView mMarketText;
	
	private TextView mNameText;
	private TextView mAddressText;
	private TextView mContactNameText;
	private ImageView mCallPhoneImage;
	
	private String id;

	private ImageView mCallChatImage;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_supplier_detail);
		initData();
		initView();
		setUpView();
		requestData();
	}

	private void initData() {
	    id = getIntent().getStringExtra(ExtraName.id);
    }

    private void requestData() {
	    startLoadingDialog();
	    RequestService.getInstance().getSellerDetail(this, id, BusinessSellerDetailEntity.class, new RequestListener() {
            
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if(resultData.isOk()){
                    BusinessSellerDetailEntity entity = (BusinessSellerDetailEntity) resultData;
                    updateUi(entity);
                }else{
                    showToast(resultData.getMsg());
                }
            }
            
            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
            }
        });
    }
    protected void updateUi(BusinessSellerDetailEntity entity) {
        final BusinessSellerDetail data = entity.getData();
        
        mMarketText.setText(data.getSupplymarket());
        mSupplyText.setText(data.getCatname());
        
        mNameText.setText(data.getUnit());
        mAddressText.setText(data.getAddress());
        mContactNameText.setText("联系人："+data.getRealname());
        mCallPhoneImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DeviceHelper.callPhone(SupplierDetailActivity.this, data.getMobile(), data.getKey());
			}
		});
        mCallChatImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				IMFactory.openChat(SupplierDetailActivity.this, data.getUserid());
			}
		});
        
		ProductGridviewAdapter1<Supply> adapter = new ProductGridviewAdapter1<>(this, data.getSupplys(), false);
		mProductGv.setAdapter(adapter);
		//禁止自己发布订单联系方式
		if (AccountManager.getUserId().equals(data.getUserid())) {
			mCallPhoneImage.setVisibility(View.GONE);
			mCallChatImage.setVisibility(View.GONE);
		}
    }

    private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mProductGv = (MeasureGridView) this.findViewById(R.id.gv_supply_else);
		mListView = (XListView) this.findViewById(R.id.listview_data_list);
		mMarketText = (TextView) this.findViewById(R.id.textview_market_name);
		mSupplyText = (TextView) this.findViewById(R.id.textview_supply_name);
		mNameText = (TextView) this.findViewById(R.id.textview_name);
		mAddressText = (TextView) this.findViewById(R.id.textview_address);
		mContactNameText = (TextView) this.findViewById(R.id.textview_contact_name);
		mCallPhoneImage = (ImageView) this.findViewById(R.id.imageview_call_phone);
		mCallChatImage = (ImageView) this.findViewById(R.id.imageview_call_chat);
	}

	private void setUpView() {
		mTopbarView.setCenterText("供货商");
		mTopbarView.setLeftView(true, true);

//		List<String> list = new ArrayList<>();
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		list.add("");
//		
//		ProductGridviewAdapter<String> adapter = new ProductGridviewAdapter<>(this, list, false);
//		mProductGv.setAdapter(adapter);
//		
//		MerchantsDetailListAdapter<String> adapter1 = new MerchantsDetailListAdapter<>(this, list, false);
//		mListView.setAdapter(adapter1);
		
	}
	
}
